package com.yigang.controller;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.yigang.entity.User;
import com.yigang.service.UserService;
import com.yigang.utils.UserConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yigang.entity.Admin;
import com.yigang.service.AdminService;
import com.yigang.utils.ResultUtil;
import org.springframework.web.bind.annotation.RestController;

/**
 * @PACKAGE_NAME: com.yigang.controller
 * @USER:
 * @DATE: 2020/12/18 14:11
 */
@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    /*
     * 登陆
     */
    @RequestMapping("/login")
    public ResultUtil login(String username, String password, HttpServletRequest request, HttpSession session) {
        User user = userService.login(username, password);
        if (null != user) {
            session.setAttribute(UserConst.LOGIN_USER_INFO, JSON.toJSONString(user));
            return ResultUtil.ok(user.getId());
        } else {
            return ResultUtil.error("用户名密码错误");
        }
    }

    /*
     * 获取当前登陆人
     */
    @RequestMapping("/getSessionUser")
    public ResultUtil getSessionUser(HttpSession session) {
        Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject(JSON.toJSONString(attribute), User.class);
            return ResultUtil.ok(user);
        } else {
            return ResultUtil.error();
        }
    }
}
